/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gla.centralhotel.dao;

import gla.centralhotel.dao.interfaces.IZipcodeDAO;
import gla.centralhotel.model.Zipcode;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Rivo
 */
@Stateless
public class ZipcodeDAO implements IZipcodeDAO {
    @PersistenceContext
    private EntityManager em;

    public Zipcode create(Zipcode zipcode) {
        em.persist(zipcode);
        return zipcode;
    }

    public Zipcode edit(Zipcode zipcode) {
        em.merge(zipcode);
        return zipcode;
    }

    public void remove(Zipcode zipcode) {
        em.remove(em.merge(zipcode));
    }

    public Zipcode find(Object id) {
        return em.find(gla.centralhotel.model.Zipcode.class, id);
    }

    public List<Zipcode> findAll() {
        return em.createQuery("select object(o) from Zipcode as o").getResultList();
    }

    public Zipcode findZipcodeByZipcodeAndCity(String zipcode, String city) {
        String query = "select zp from Zipcode as zp where zp.zipcode = :zipcode and zp.city = :city";
        Query q = em.createQuery(query);
        q.setParameter("zipcode", zipcode);
        q.setParameter("city", city);
        return (Zipcode) q.getSingleResult();        
    }

}
